import { observable, action ,toJS} from 'mobx';
import { ListViewPage, RouteLayout, ShareAuth } from 'weaver-mobile-page';
import * as Apis from "../../api/customerApis";
import { Toast, Modal, Tools, Bubble } from 'weaver-mobile';
import * as adapter from '../../constants/adapter';
const alert = Modal.alert;
const getLabel = Tools.getLabel;
export default class CustomerShareListStore {
    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    comObj = {};
    params = {};
    @observable listTab = [];
    @observable listTabKey = "";
    @observable listTabIndex = "my";
    @observable showCheck = false;
    @observable list = {};
    @observable canEdit = false;
    @observable createShareLayout = {};
    @observable initResponsed = false;// 是否已经得到初始化数据的响应
    @observable initSuccessed = false;// 初始化数据是否获取成功
    @observable initFailedMsg = "";   // 初始化处理失败的原因
    @observable shareAuthRoute = {};
    @observable shareConditions = [];
    @observable shareParams = {};
    @observable shareform = {};

    @action
    doNew = (comObj, params) => {
        this.comObj = comObj;
        this.params = params;
    }

    @action
    doInit = () => {
        let params = this.params;
        Apis.getHearderTab(params).then(data => {
            Apis.getCreaterAuthorFormCondition({ showlevel: 1 }).then(
                data => {
                    this.shareConditions = data.data[0];
                    this.shareConditions1 = data.data[1];
                }
            )
            this.doInitCallBack(params, data);
        });
    }

    @action
    doInitCallBack = (params, data) => {
        this.initResponsed = true;
        this.initSuccessed = true;
        const currentPath = this.comObj.props.location.pathname;
        this.list = new ListViewPage.Store({ route: currentPath, id: 'customerShareList' });
        this.createShareLayout = new RouteLayout.Store({ route: currentPath, id: 'createShareLayout' });
        this.shareAuthRoute = new ShareAuth.Store({ route: currentPath, id: 'list' });
        this.listTab = data.tabs;
        this.listTabKey = data.keyParam;
        this.canEdit = data.canEdit;
        this.doSearch();
    }

    @action
    doSearch = () => {
        const params = { id: this.params.id };
        Apis.getChnceShareListResult(params).then(data => {
            const dataKey = data.datas;
            this.list.clearData();
            this.list.getData({ dataKey });
        });
    }

    @action
    topTabChange = (selectedIndex) => {
        this.listTabIndex = selectedIndex;
        this.doSearch();
    }

    @action
    doClear = () => {
        this.initResponsed = false;
        this.initSuccessed = false;
        this.initFailedMsg = "";
        this.listTabIndex = "my";
        this.list.clearData();
    }

    @action
    deleteShare = (data) => {
        if (data.isdefault == 0) {
            Toast.fail(getLabel('390952', "不能删除默认共享权限"), 2);
            return;
        }
        alert('', getLabel('83600', '您确认要删除吗？'), [
            { text: getLabel('201', '取消'), onPress: () => { } },
            {
                text: getLabel('83446', '确定'), onPress: () => {
                    const params = { ids: data.id ,sellChance: this.params.id};
                    Toast.loading(getLabel('388626', '正在保存，请稍后...'), 0);
                    Apis.deleteSellChanceAuths(params).then(data => {
                        Toast.hide();
                        this.doSearch();
                    });
                }
            },
        ]);
    }
    doSave = (data) => {
        
        if (this.shareAuthRoute.optionsForm.doValidate().pass) {
            let params={...data,sellChance: this.params.id}
            if(data.jobtitlelevel){
                params={...data,sellChance: this.params.id,jobtitlelevel:data.jobtitlelevel.jobtitlelevel}    
                let  jobtitlesubcompanyinfo = toJS(data.jobtitlelevel.jobtitlesubcompany);    
                if(jobtitlesubcompanyinfo){
                    let jobtitlesubcompany='',jobtitlesubcompanyspan='';
                    for(let i in jobtitlesubcompanyinfo ){
                        jobtitlesubcompany =jobtitlesubcompany+jobtitlesubcompanyinfo[i].id+',';
                        jobtitlesubcompanyspan = jobtitlesubcompanyspan+jobtitlesubcompanyinfo[i].name+',';
                      
                    }
                    params.jobtitlesubcompany = jobtitlesubcompany;
                    params.jobtitlesubcompanyspan = jobtitlesubcompanyspan
                 
                }
                let  jobtitledepartmentinfo = toJS(data.jobtitlelevel.jobtitledepartment); 
                if(jobtitledepartmentinfo){
                    let jobtitledepartment='',jobtitledepartmentspan='';
                    for(let i in jobtitledepartmentinfo ){
                        jobtitledepartment =jobtitledepartment+jobtitledepartmentinfo[i].id+',';
                        jobtitledepartmentspan = jobtitledepartmentspan+jobtitledepartmentinfo[i].name+',';                       
                    }
                    params.jobtitledepartment = jobtitledepartment;
                    params.jobtitledepartmentspan = jobtitledepartmentspan              
                }
                
            }
           

            Apis.addChanceShare(params).then(result => {
                if (result.status === "success") {
                    Bubble.success(getLabel('83551', "保存成功"), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
                        if (window.crmGlobal) {
                            window.crmGlobal.doSearch();
                        }
                        this.doInit();
                    });
                } else {
                    Bubble.error(getLabel(22620, '保存失败'), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
                        if (window.crmGlobal) {
                            window.crmGlobal.doSearch();
                        }
                        this.doInit();
                    });
                }
            });
        }

    }
}